package net.zhengxinyun.performance.controller.setTime;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.SetTimeEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.SetTimeFilter;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.setTime.SetTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


/**
 * 设置时间段
 */
@RestController
@RequestMapping("/setTime")
public class SetTimeController {
    @Autowired
    private SetTimeService service;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/update")
    public Result<String> updateSetTime(@RequestBody SetTimeParam setTimeParam) {
        if (tokenService.compareToken(setTimeParam.tokenEntity.getValue())) {
            String str = service.updateTime(setTimeParam.setTimeFilter);
            if ("更新成功".equals(str)) {
                return ResultUtils.getSuccessResults();
            }
            return ResultUtils.getResults(Result.Status.ERROR, str);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    //回显当前时间段设置 1市对县 2县对乡 3 当前年份
    @PostMapping("/select")
    public Result<HashMap<String, Object>> select(@RequestBody SetTimeParam setTimeParam) {
        if (tokenService.compareToken(setTimeParam.tokenEntity.getValue())) {
            HashMap<String, Object> map = new HashMap<>();
            SetTimeEntity setTimeEntity = service.findByIndexClass(setTimeParam.setTimeFilter.getIndexClass());
            if (setTimeEntity != null) {
                map.put("entity", setTimeEntity);
                return ResultUtils.getSuccessResults(map);
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }


}

class SetTimeParam {
    public TokenEntity tokenEntity;
    public SetTimeEntity setTimeEntity;
    public SetTimeFilter setTimeFilter;
}
